package com.wallj.livechatclient;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;


public class MainActivity extends Activity implements View.OnClickListener {
    EditText et_input;
    ListView lv_msg;
    ConnectDialog connectDialog;
    ClientThread clientThread;
    AdapterMessage adapterMessage;
    ArrayList<EntityMessage> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_input = (EditText) findViewById(R.id.et_input);
        lv_msg = (ListView) findViewById(R.id.lv_msg);
        adapterMessage = new AdapterMessage(this,datas);
    }

    public void openClient(View view) {
        if (connectDialog == null) {
            connectDialog = new ConnectDialog(this, this);
        }
        connectDialog.show();
    }

    private class ClientThread extends Thread {
        private String ip;
        private int port;
        private Socket clientSocket;
        PrintWriter out = null;

        public ClientThread(String ip, int port) {
            this.ip = ip;
            this.port = port;
        }

        @Override
        public void run() {
            super.run();
            Socket server = null;
            BufferedReader in = null;
            try {
                server = new Socket(ip, port);
                InputStream inputStream = server.getInputStream();
                in = new BufferedReader(new InputStreamReader(
                        inputStream));

                out = new PrintWriter(server.getOutputStream());
                while (true) {
                    String msg = in.readLine();
                    if (msg != null) {
                        if (msg.equals("end")) {
                            break;
                        }
                        Message uimsg = handler.obtainMessage();
                        uimsg.what = MSG_UI_REFRESH_MSG;
                        uimsg.obj = msg;
                        uimsg.sendToTarget();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    server.close();
                    in.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        public void sendMessage(String message) {
            out.write(message);
            out.flush();
        }
    }

    public static final int MSG_UI_REFRESH_MSG = 0X01;
    public static final int MSG_UI_SHOW_TOAST = 0X02;
    private android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_UI_REFRESH_MSG: {
                    String message = (String) msg.obj;
                    break;
                }
            }
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_connect:
                if (connectDialog != null) {
                    String ip = connectDialog.getIp();
                    int port = Integer.parseInt(connectDialog.getPort());
                    clientThread = new ClientThread(ip, port);
                    clientThread.start();
                    connectDialog.dismiss();
                }
                break;
        }
    }
}
